use std::time::Duration;

use futures_timer::Delay;
use mlua::prelude::*;

fn hello(_: &Lua, name: String) -> LuaResult<()> {
    println!("hello, {}!", name);
    Ok(())
}

async fn sleep_ms(_lua: &Lua, n: u64) -> LuaResult<&'static str> {
    Delay::new(Duration::from_millis(n)).await;
    Ok("done")
}

#[mlua::lua_module]
fn mymodule(lua: &Lua) -> LuaResult<LuaTable> {
    let exports = lua.create_table()?;
    exports.set("hello", lua.create_function(hello)?)?;
    exports.set("sleep_ms", lua.create_async_function(sleep_ms)?)?;
    Ok(exports)
}
